package com.qxf.sell.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qxf.sell.mapper.UserInfoMapper;
import com.qxf.sell.model.UserInfo;
import com.qxf.sell.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author qxf
 * @since 2021-04-30
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    public List<UserInfo> findAllUser() {
        return userInfoMapper.findAllUser();
    }

    @Override
    public IPage<UserInfo> getUserByPage(IPage<UserInfo> page, QueryWrapper<UserInfo> wrapper) {
        return userInfoMapper.selectPage(page, wrapper);
    }

    @Override
    public int register(String name, int age, String password) {
        boolean isExist = isUserExist(name);
        if (isExist) {
            return 0;
        }

        UserInfo userInfo = new UserInfo();
        userInfo.setName(name);
        userInfo.setAge(age);
        userInfo.setPassword(password);

        return userInfoMapper.insert(userInfo);
    }

    @Override
    public boolean isUserExist(String name) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserInfo::getName, name);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);

        return userInfo != null;
    }

    @Override
    public boolean login(String name, String password) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(UserInfo::getName, name);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);

        if (userInfo != null) {
            return password.equals(userInfo.getPassword());
        }

        return false;
    }


}
